﻿unit uTcpDef;

interface

uses
  Classes, DLDatObj, DLDatLst, DLRttiDatObj, uViDef;

type
  TReqHead = packed record
    Flag: LongInt;
    Cmd: LongInt;
    DatLen: LongInt;
  end;
  PReqHead = ^TReqHead;

  TRespHead = packed record
    DatLen: LongInt;
  end;
  PRespHead = ^TRespHead;

type
  TCustomResponse = class(TDLCustomRttiDataObject)
  private
    tvSuccess: Boolean;
    tvErrMsg: AnsiString;
  published
    property Success: Boolean read tvSuccess write tvSuccess;
    property ErrMsg: AnsiString read tvErrMsg write tvErrMsg;
  end;

type
  TLongIntList = class(TDLCustomDataObject)
  private
    tvItems: array of LongInt;
    function GetCount: LongInt;
    function GetItems(avIndex: LongInt): LongInt;
  protected
    procedure DoRead(avNameIndex: TNameIndex;
      avPointer: Pointer; avSize: LongInt); override;
  public
    procedure Clear; override;
    procedure SaveToStream(avOutput: TStream); override;
    property Count: LongInt read GetCount;
    property Items[avIndex: LongInt]: LongInt read GetItems;
    procedure Add(avValue: LongInt);
  end;

type
  TGetVehicleRequest = class(TDLCustomRttiDataObject)
  private
    tvCityCode, tvToCityCode: TLongIntList;
    tvSiJiXingMing, tvChePaiHaoMa, tvSuiCheShouJi,
    tvCheZhang, tvDunWei, tvRongJi, tvCheKuan, tvCheGao, tvBeiZhu: AnsiString;
    tvCar_InfoType, tvSuoShuYongHu, tvIndexFrom, tvGetCount: LongInt;
    tvCar_Source, tvState: ShortInt;
    procedure SetCityCode(avValue: TLongIntList);
    procedure SetToCityCode(avValue: TLongIntList);
  public
    constructor Create;
    destructor Destroy; override;
  published
    property Car_InfoType: LongInt read tvCar_InfoType write tvCar_InfoType;
    property Car_Source: ShortInt read tvCar_Source write tvCar_Source;
    property SuoShuYongHu: LongInt read tvSuoShuYongHu write tvSuoShuYongHu;
    property State: ShortInt read tvState write tvState; //-1全部, 0未开通, 1开通
    property CityCode: TLongIntList read tvCityCode write SetCityCode;
    property ToCityCode: TLongIntList read tvToCityCode write SetToCityCode;
    property SiJiXingMing: AnsiString read tvSiJiXingMing write tvSiJiXingMing;
    property ChePaiHaoMa: AnsiString read tvChePaiHaoMa write tvChePaiHaoMa;
    property SuiCheShouJi: AnsiString read tvSuiCheShouJi write tvSuiCheShouJi;
    property CheZhang: AnsiString read tvCheZhang write tvCheZhang;
    property DunWei: AnsiString read tvDunWei write tvDunWei;
    property RongJi: AnsiString read tvRongJi write tvRongJi;
    property CheKuan: AnsiString read tvCheKuan write tvCheKuan;
    property CheGao: AnsiString read tvCheGao write tvCheGao;
    property BeiZhu: AnsiString read tvBeiZhu write tvBeiZhu;
    property IndexFrom: LongInt read tvIndexFrom write tvIndexFrom;
    property GetCount: LongInt read tvGetCount write tvGetCount;
    procedure SetParam(avCar_InfoType: LongInt; avCar_Source: ShortInt;
      avSuoShuYongHu: LongInt; avState: ShortInt;
      const avCityCode, avToCityCode: array of LongInt;
      const avSiJiXingMing, avChePaiHaoMa, avSuiCheShouJi,
      avCheZhang, avDunWei, avRongJi, avCheKuan, avCheGao, avBeiZhu: AnsiString;
      avIndexFrom, avGetCount: LongInt);
  end;

  TGetVehicleResponse = class(TCustomResponse)
  private
    tvMatchCount: LongInt;
    tvItems: TVehicleList;
    procedure SetItems(avValue: TVehicleList);
  public
    constructor Create;
    destructor Destroy; override;
  published
    property MatchCount: LongInt read tvMatchCount write tvMatchCount;
    property Items: TVehicleList read tvItems write SetItems;
  end;

type
  TAddVehicleRequest = TVehicleItem;

  TAddVehicleResponse = class(TCustomResponse)
  private
    tvCheLiangBianHao: LongInt;
  published
    property CheLiangBianHao: LongInt read tvCheLiangBianHao write tvCheLiangBianHao;
  end;

type
  TUpdateVehicleRequest = TVehicleItem;

  TUpdateVehicleResponse = TCustomResponse;

type
  TDeleteVehicleRequest = class(TDLCustomRttiDataObject)
  private
    tvCheLiangBianHao: LongInt;
    tvSaveForReUse: Boolean;
  published
    property CheLiangBianHao: LongInt read tvCheLiangBianHao write tvCheLiangBianHao;
    property SaveForReUse: Boolean read tvSaveForReUse write tvSaveForReUse;
  end;

  TDeleteVehicleResponse = TCustomResponse;

type
  TDeleteVehicleListRequest = class(TDLCustomRttiDataObject)
  private
    tvCheLiangBianHao: TLongIntList;
    tvSaveForReUse: Boolean;
    procedure SetCheLiangBianHao(avValue: TLongIntList);
  public
    constructor Create;
    destructor Destroy; override;
  published
    property CheLiangBianHao: TLongIntList read tvCheLiangBianHao write SetCheLiangBianHao;
    property SaveForReUse: Boolean read tvSaveForReUse write tvSaveForReUse;
  end;

  TDeleteVehicleListResponse = class(TCustomResponse)
  private
    tvSuccessCount: LongInt;
  published
    property SuccessCount: LongInt read tvSuccessCount write tvSuccessCount;
  end;

type
  TAutoLocItem = class(TDLCustomRttiDataObject)
  private
    tvSuiCheShouJi: AnsiString;
    tvCheLiangBianHao: LongInt;
    tvSuoShuYongHu: LongInt;
  published
    property CheLiangBianHao: LongInt
      index 1 read tvCheLiangBianHao write tvCheLiangBianHao;
    property SuoShuYongHu: LongInt
      index 2 read tvSuoShuYongHu write tvSuoShuYongHu;
    property SuiCheShouJi: AnsiString
      index 71 read tvSuiCheShouJi write tvSuiCheShouJi;
  end;

  TAutoLocList = class(TDLCustomDataList)
  protected
    function NewItem: TDLCustomDataObject; override;
  end;

type
  TGetAutoLocRequest = class(TDLCustomRttiDataObject)
  private
    tvGetCount: LongInt;
    tvAutoLocTimeFrom, tvAutoLocTimeTo: Shortint;
  published
    property AutoLocTimeFrom: ShortInt read tvAutoLocTimeFrom write tvAutoLocTimeFrom;
    property AutoLocTimeTo: ShortInt read tvAutoLocTimeTo write tvAutoLocTimeTo;
    property GetCount: LongInt read tvGetCount write tvGetCount;
  end;

  TGetAutoLocResponse = class(TCustomResponse)
  private
    tvItems: TAutoLocList;
    procedure SetItems(avValue: TAutoLocList);
  public
    constructor Create;
    destructor Destroy; override;
  published
    property Items: TAutoLocList read tvItems write SetItems;
  end;

type
  TUnAcceptItem = class(TDLCustomRttiDataObject)
  private
    tvSuiCheShouJi: AnsiString;
  published
    property SuiCheShouJi: AnsiString
      index 71 read tvSuiCheShouJi write tvSuiCheShouJi;
  end;

  TUnAcceptList = class(TDLCustomDataList)
  protected
    function NewItem: TDLCustomDataObject; override;
  end;

type
  TGetUnAcceptRequest = class(TDLCustomRttiDataObject)
  private
    tvGetCount: LongInt;
    tvReset: Boolean;
  published
    property GetCount: LongInt read tvGetCount write tvGetCount;
    property Reset: Boolean read tvReset write tvReset;
  end;

  TGetUnAcceptResponse = class(TCustomResponse)
  private
    tvItems: TUnAcceptList;
    procedure SetItems(avValue: TUnAcceptList);
  public
    constructor Create;
    destructor Destroy; override;
  published
    property Items: TUnAcceptList read tvItems write SetItems;
  end;

type
  TWebVehicleItem = class(TDLCustomRttiDataObject)
  private
    tvCar_AddTime, tvDingWeiShiJian: TDateTime;
    tvOwnerUserName, tvOwnerCityName: AnsiString;
    tvChePaiHaoMa, tvSiJiXingMing, tvSuiCheShouJi, tvCheLiangWeiZhi: AnsiString;
    tvCheLiangBianHao, tvOwnerCityCode, tvZhuCeFou: LongInt;
  published
    property CheLiangBianHao: LongInt //车辆编号，自增ID
      index 1 read tvCheLiangBianHao write tvCheLiangBianHao;
    property OwnerUserName: AnsiString
      index 3 read tvOwnerUserName write tvOwnerUserName;
    property OwnerCityCode: LongInt
      index 4 read tvOwnerCityCode write tvOwnerCityCode;
    property OwnerCityName: AnsiString
      index 5 read tvOwnerCityName write tvOwnerCityName;
    property ChePaiHaoMa: AnsiString //100 车牌号
      index 21 read tvChePaiHaoMa write tvChePaiHaoMa;
    property SiJiXingMing: AnsiString //50 司机姓名
      index 70 read tvSiJiXingMing write tvSiJiXingMing;
    property SuiCheShouJi: AnsiString //15 随车手机
      index 71 read tvSuiCheShouJi write tvSuiCheShouJi;
    property ZhuCeFou: LongInt //定位服务状态。未开通-1，开通中1（鉴权短信已发送，但尚未回复），已开通3
      index 80 read tvZhuCeFou write tvZhuCeFou;
    property DingWeiShiJian: TDateTime //最后定位时间
      index 82 read tvDingWeiShiJian write tvDingWeiShiJian;
    property CheLiangWeiZhi: AnsiString //80 当前位置（省市）
      index 85 read tvCheLiangWeiZhi write tvCheLiangWeiZhi;
    property Car_AddTime: TDateTime //添加车辆的时间
      index 90 read tvCar_AddTime write tvCar_AddTime;
  end;

  TWebVehicleList = class(TDLCustomDataList)
  protected
    function NewItem: TDLCustomDataObject; override;
  end;

type
  TGetWebVehicleRequest = class(TDLCustomRttiDataObject)
  private
    tvSuiCheShouJi, tvChePaiHaoMa: AnsiString;
    tvCheLiangBianHao, tvOwnerCityCode, tvIndexFrom, tvGetCount: LongInt;
  published
    property CheLiangBianHao: LongInt read tvCheLiangBianHao write tvCheLiangBianHao;
    property OwnerCityCode: LongInt read tvOwnerCityCode write tvOwnerCityCode;
    property SuiCheShouJi: AnsiString read tvSuiCheShouJi write tvSuiCheShouJi;
    property ChePaiHaoMa: AnsiString read tvChePaiHaoMa write tvChePaiHaoMa;
    property IndexFrom: LongInt read tvIndexFrom write tvIndexFrom;
    property GetCount: LongInt read tvGetCount write tvGetCount;
  end;

  TGetWebVehicleResponse = class(TCustomResponse)
  private
    tvMatchCount: LongInt;
    tvItems: TWebVehicleList;
    procedure SetItems(avValue: TWebVehicleList);
  public
    constructor Create;
    destructor Destroy; override;
  published
    property MatchCount: LongInt read tvMatchCount write tvMatchCount;
    property Items: TWebVehicleList read tvItems write SetItems;
  end;

type
  TUpdateStateRequest = class(TDLCustomRttiDataObject)
  private
    tvSuiCheShouJi: AnsiString;
    tvZhuCeFou: LongInt;
  published
    property SuiCheShouJi: AnsiString read tvSuiCheShouJi write tvSuiCheShouJi;
    property ZhuCeFou: LongInt read tvZhuCeFou write tvZhuCeFou;
  end;

  TUpdateStateResponse = TCustomResponse;

type
  TUpdateLocationRequest = class(TDLCustomRttiDataObject)
  private
    tvDingWeiShiJian: TDateTime;
    tvJingDu, tvWeiDu, tvCheLiangWeiZhi, tvWeiZhiMiaoShu: AnsiString;
    tvCheLiangBianHao, tvErrorCode, tvZhuCeFou, tvCityCode: LongInt;
  published
    property CheLiangBianHao: LongInt read tvCheLiangBianHao write tvCheLiangBianHao;
    property DingWeiShiJian: TDateTime read tvDingWeiShiJian write tvDingWeiShiJian;
    property ErrorCode: LongInt read tvErrorCode write tvErrorCode;
    property ZhuCeFou: LongInt read tvZhuCeFou write tvZhuCeFou;
    property JingDu: AnsiString read tvJingDu write tvJingDu;
    property WeiDu: AnsiString read tvWeiDu write tvWeiDu;
    property CheLiangWeiZhi: AnsiString read tvCheLiangWeiZhi write tvCheLiangWeiZhi;
    property WeiZhiMiaoShu: AnsiString read tvWeiZhiMiaoShu write tvWeiZhiMiaoShu;
    property CityCode: LongInt read tvCityCode write tvCityCode;
  end;

  TUpdateLocationResponse = TCustomResponse;

const
  gcPort = 544;
  gcFlag = $50434456; //VDCP
  gcCmdGetVehicle     = 10;
  gcCmdAddVehicle     = 11;
  gcCmdUpdateVehicle  = 12;
  gcCmdDeleteVehicle  = 13;
  gcCmdDelVehicleList = 14;
  gcCmdGetAutoLoc     = 21;
  gcCmdGetUnAccept    = 22;
  gcCmdGetWebVehicle  = 23;
  gcCmdUpdateState    = 31;
  gcCmdUpdateLocation = 32;

implementation

uses
  SysUtils;

{ TLongIntList }

procedure TLongIntList.Clear;
begin
  SetLength(Self.tvItems, 0);
end;

procedure TLongIntList.SaveToStream(avOutput: TStream);
begin
  if Length(Self.tvItems) > 0 then
    Self.Write(0, Pointer(Self.tvItems), SizeOf(LongInt)*Length(Self.tvItems), avOutput);
  Self.WriteEndFlag(avOutput);
end;

procedure TLongIntList.DoRead(avNameIndex: TNameIndex;
  avPointer: Pointer; avSize: LongInt);
begin
  if (avNameIndex = 0) and (avSize mod SizeOf(LongInt) = 0) then
    begin
      SetLength(Self.tvItems, avSize div SizeOf(LongInt));
      Move(avPointer^, Pointer(Self.tvItems)^, avSize);
    end;
end;

function TLongIntList.GetCount: LongInt;
begin
  Result := Length(Self.tvItems)
end;

function TLongIntList.GetItems(avIndex: LongInt): LongInt;
begin
  if (avIndex >= 0) and (avIndex < Length(Self.tvItems)) then
    Result := Self.tvItems[avIndex]
  else
    raise Exception.Create('Out of range.');
end;

procedure TLongIntList.Add(avValue: LongInt);
var
  i: LongInt;
begin
  i := Length(Self.tvItems);
  SetLength(Self.tvItems, i+1);
  Self.tvItems[i] := avValue;
end;

{ TGetVehicleRequest }

constructor TGetVehicleRequest.Create;
begin
  inherited;
  Self.tvCityCode := TLongIntList.Create;
  Self.tvToCityCode := TLongIntList.Create;
end;

destructor TGetVehicleRequest.Destroy;
begin
  Self.tvToCityCode.Free;
  Self.tvCityCode.Free;
  inherited;
end;

procedure TGetVehicleRequest.SetCityCode(avValue: TLongIntList);
begin
  Self.tvCityCode.Assign(avValue);
end;

procedure TGetVehicleRequest.SetToCityCode(avValue: TLongIntList);
begin
  Self.tvToCityCode.Assign(avValue);
end;

procedure TGetVehicleRequest.SetParam(avCar_InfoType: LongInt;
  avCar_Source: ShortInt; avSuoShuYongHu: LongInt; avState: ShortInt;
  const avCityCode, avToCityCode: array of LongInt;
  const avSiJiXingMing, avChePaiHaoMa, avSuiCheShouJi, avCheZhang,
  avDunWei, avRongJi, avCheKuan, avCheGao, avBeiZhu: AnsiString;
  avIndexFrom, avGetCount: LongInt);
var
  i: LongInt;
begin
  Self.Car_InfoType := avCar_InfoType;
  Self.Car_Source := avCar_Source;
  Self.SuoShuYongHu := avSuoShuYongHu;
  Self.State := avState;
  for i := Low(avCityCode) to High(avCityCode) do
    Self.CityCode.Add(avCityCode[i]);
  for i := Low(avToCityCode) to High(avToCityCode) do
    Self.ToCityCode.Add(avToCityCode[i]);
  Self.SiJiXingMing := avSiJiXingMing;
  Self.ChePaiHaoMa := avChePaiHaoMa;
  Self.SuiCheShouJi := avSuiCheShouJi;
  Self.CheZhang := avCheZhang;
  Self.DunWei := avDunWei;
  Self.RongJi := avRongJi;
  Self.CheKuan := avCheKuan;
  Self.CheGao := avCheGao;
  Self.BeiZhu := avBeiZhu;
  Self.IndexFrom := avIndexFrom;
  Self.GetCount := avGetCount;
end;

{ TGetVehicleResponse }

constructor TGetVehicleResponse.Create;
begin
  inherited;
  Self.tvItems := TVehicleList.Create;
end;

destructor TGetVehicleResponse.Destroy;
begin
  Self.tvItems.Free;
  inherited;
end;

procedure TGetVehicleResponse.SetItems(avValue: TVehicleList);
begin
  Self.tvItems.Assign(avValue);
end;

{ TDeleteVehicleListRequest }

constructor TDeleteVehicleListRequest.Create;
begin
  inherited;
  Self.tvCheLiangBianHao := TLongIntList.Create;
end;

destructor TDeleteVehicleListRequest.Destroy;
begin
  Self.tvCheLiangBianHao.Free;
  inherited;
end;

procedure TDeleteVehicleListRequest.SetCheLiangBianHao(avValue: TLongIntList);
begin
  Self.tvCheLiangBianHao.Assign(avValue);
end;

{ TAutoLocList }

function TAutoLocList.NewItem: TDLCustomDataObject;
begin
  Result := TAutoLocItem.Create;
end;

{ TGetAutoLocListResponse }

constructor TGetAutoLocResponse.Create;
begin
  inherited;
  Self.tvItems := TAutoLocList.Create;
end;

destructor TGetAutoLocResponse.Destroy;
begin
  Self.tvItems.Free;
  inherited;
end;

procedure TGetAutoLocResponse.SetItems(avValue: TAutoLocList);
begin
  Self.tvItems.Assign(avValue);
end;

{ TUnAcceptList }

function TUnAcceptList.NewItem: TDLCustomDataObject;
begin
  Result := TUnAcceptItem.Create;
end;

{ TGetUnAcceptResponse }

constructor TGetUnAcceptResponse.Create;
begin
  inherited;
  Self.tvItems := TUnAcceptList.Create;
end;

destructor TGetUnAcceptResponse.Destroy;
begin
  Self.tvItems.Free;
  inherited;
end;

procedure TGetUnAcceptResponse.SetItems(avValue: TUnAcceptList);
begin
  Self.tvItems.Assign(avValue);
end;

{ TWebVehicleList }

function TWebVehicleList.NewItem: TDLCustomDataObject;
begin
  Result := TWebVehicleItem.Create;
end;

{ TGetWebVehicleResponse }

constructor TGetWebVehicleResponse.Create;
begin
  inherited;
  Self.tvItems := TWebVehicleList.Create;
end;

destructor TGetWebVehicleResponse.Destroy;
begin
  Self.tvItems.Free;
  inherited;
end;

procedure TGetWebVehicleResponse.SetItems(avValue: TWebVehicleList);
begin
  Self.tvItems.Assign(avValue);
end;

end.